﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Controls.Primitives;
using System.IO;
using System.Diagnostics;
using System.Xml.Linq;

namespace ByrForum
{
    public partial class ReplyPopup : UserControl
    {
        public PhoneApplicationPage BasePage;
        public string replyTitle = "";
        public string replyContent = "";
        public string replyId = "";
        public string replyUserId = "";
        public ReplyPopup(TopTen basePage)
        {
            
            InitializeComponent();
            BasePage=basePage;
            this.TextReplyTitle.Text ="RE:"+ basePage.titleToDisplay;
            replyId = basePage.replyId;
            replyUserId = basePage.replyUserId;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (TextReplyTitle.Text == "" || TextReplyContent.Text == "")
            {
                MessageBox.Show("回复内容或标题不能为空！");
                return;
            }
            replyTitle = TextReplyTitle.Text;
            replyContent = TextReplyContent.Text;
            
            string url = ByrApi.apiUrl + "/article/" + ((TopTen)BasePage).board +"/post"+ ByrApi.Encode + ByrApi.appkey;
            HttpUtil.HttpPost(url, PostAriticle_Callback);
            progressBarWait.Visibility = Visibility.Visible;

            
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.CloseMeAsPopup();
        }

        private void PostAriticle_Callback(IAsyncResult asynchronousResult)
        {

            Debug.WriteLine(replyTitle + "+++" + replyContent + "++++" + replyId);
            try
            {
                HttpWebRequest hwrq = (HttpWebRequest)asynchronousResult.AsyncState;
                string content = "";
                Stream responseStream = hwrq.EndGetRequestStream(asynchronousResult);
                StreamWriter sr = new StreamWriter(responseStream);
                if (replyUserId == "")
                {
                    
                    content = replyContent + "\n来自：Windows Phone 7 客户端-alpha";
                }
                else
                {
                    content = replyContent + "\n" + ((TopTen)BasePage).appendText + "\n\n\n来自：Windows Phone 7 客户端-alpha";
                }

                sr.Write("title=" + replyTitle + "&content=" +content + "&reid="+replyId +"&signature=1" );
                sr.Close();
                responseStream.Close();
                hwrq.BeginGetResponse(PostResponse_Callback, hwrq);
            }
            catch (Exception e)
            {
                Debug.WriteLine("POST STRING FAILED:" + e.Message);
            }
        }
        private void PostResponse_Callback(IAsyncResult asynchronousResult)
        {
            try
            {
                string result = HttpUtil.ReadToEnd(asynchronousResult);
                XDocument doc = XDocument.Parse(result);
                Debug.WriteLine(result);
                foreach (XElement item in doc.Descendants("error").Nodes())
                {
                    if (item.Name == "msg")
                    {
                        this.Dispatcher.BeginInvoke(
                            () =>
                            {
                                MessageBox.Show("回复失败："+item.Value);
                                progressBarWait.Visibility = Visibility.Collapsed;
                                
                            }
                            );


                        return;
                    }
                }
                this.Dispatcher.BeginInvoke(
                    () => {
                        MessageBox.Show("回复成功！");
                        ((TopTen)BasePage).GoToPage(((TopTen)BasePage).current_page);
                        progressBarWait.Visibility = Visibility.Visible;
                        this.CloseMeAsPopup();
                    }
                    );
                    
               
                
            }
            catch { }
            //HttpWebRequest hwrq = (HttpWebRequest)asynchronousResult.AsyncState;
            //HttpWebResponse response = (HttpWebResponse)hwrq.EndGetResponse(asynchronousResult);
            //Debug.WriteLine("get callback");
            //Stream responseStream = response.GetResponseStream();
            //StreamReader sr = new StreamReader(responseStream);
            //rr = sr.ReadToEnd();
            

        }
    }
}